package user.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

import com.example.xfengshopping.R;
import user.fragment.GoodsItem;
import user.fragment.ImageDownloader;

public class OrderGoodsAdapter extends RecyclerView.Adapter<OrderGoodsAdapter.GoodsViewHolder> {

    private final List<OrderItem.OrderGoodsItem> goodsItemList;

    public OrderGoodsAdapter(List<OrderItem.OrderGoodsItem> goodsItemList) {
        this.goodsItemList = goodsItemList;
    }

    @NonNull
    @Override
    public GoodsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_good, parent, false);
        return new GoodsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull GoodsViewHolder holder, int position) {
        OrderItem.OrderGoodsItem item = goodsItemList.get(position);

        // 使用自定义异步任务下载图片
        new ImageDownloader(holder.ivCover).execute(item.getGoodsCoverImg());

        holder.tvName.setText(item.getGoodsName());
        holder.tvPrice.setText("¥" + item.getSellingPrice());
    }

    @Override
    public int getItemCount() {
        return goodsItemList.size();
    }

    static class GoodsViewHolder extends RecyclerView.ViewHolder {
        ImageView ivCover;
        TextView tvName;
        TextView tvPrice;

        public GoodsViewHolder(@NonNull View itemView) {
            super(itemView);
            ivCover = itemView.findViewById(R.id.iv_cover);
            tvName = itemView.findViewById(R.id.tv_name);
            tvPrice = itemView.findViewById(R.id.tv_price);
        }
    }
}
